package com.guldan.jaina.task;

import com.guldan.jaina.utils.validating.CheckIdCard;
import com.guldan.mapper.db.entity.CitizenInfo;
import com.guldan.mapper.db.entity.CitizenInfoExample;
import com.guldan.mapper.db.mapper.CitizenInfoMapperExt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by Silas.
 * Date: 2017/1/23
 * Time: 17:39
 */
@Component
public class CitizenAgeUpdateHandler {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private CitizenInfoMapperExt citizenInfoMapperExt;

    public void handler() {
        long begin = System.currentTimeMillis();

        CitizenInfoExample example = new CitizenInfoExample();
        CitizenInfoExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo(false);
        example.setOrderByClause("citizen_info_id asc");

        int total = citizenInfoMapperExt.countByExample(example);

        int offset = 0, limit = 1000;
        while (offset < total) {

            List<CitizenInfo> list = citizenInfoMapperExt.selectByExampleWithPaging(example, offset, limit);
            for (CitizenInfo dto : list) {
                CheckIdCard cic = new CheckIdCard(dto.getIdentNumber());
                dto.setAge(cic.getAge());
                if (cic.getGenderCode() == 1)
                    dto.setGenderCode("100");
                else
                    dto.setGenderCode("101");
                citizenInfoMapperExt.updateByPrimaryKeySelective(dto);
            }
            offset += limit;
            if (list.size() < limit) {
                offset = total;
            }
        }

        logger.info("CitizenAgeUpdateHandler.handler.cost:" + Long.valueOf(System.currentTimeMillis() - begin));
    }

}
